// src/store/reviewStore.js
import { reactive } from "vue";
import { vToast } from "@/plugins/vToast";

// 评价状态映射
export const reviewStatusMap = {
  0: "待审核",
  1: "已发布",
  2: "已隐藏",
  3: "已删除"
};

// 评价类型映射
export const reviewTypeMap = {
  1: "商品评价",
  2: "服务评价",
  3: "店铺评价"
};

// 星级映射
export const starRatingMap = {
  1: "★☆☆☆☆",
  2: "★★☆☆☆",
  3: "★★★☆☆",
  4: "★★★★☆",
  5: "★★★★★"
};

// 模拟评价数据
const reviews = reactive([
  {
    id: "PJ20230701001",
    createTime: 1688205600000,
    userId: 1001,
    userName: "张三",
    userAvatar: "/images/avatar1.webp",
    productId: "P001",
    productName: "iPhone 16",
    productImage: "/images/iphone.webp",
    content: "手机非常好用，运行流畅，拍照效果很棒！",
    images: [
      "/images/iphone.webp",
      "/images/iphone.webp"
    ],
    rating: 5,
    type: 1,
    status: 1,
    reply: "感谢您的支持，我们会继续努力提供更好的产品和服务！",
    replyTime: 1688292000000
  },
  {
    id: "PJ20230701002",
    createTime: 1688209200000,
    userId: 1002,
    userName: "李四",
    userAvatar: "/images/avatar2.webp",
    productId: "P002",
    productName: "MacBook Pro 16",
    productImage: "/images/mackbook.jpg",
    content: "性能强劲，屏幕效果出色，但电池续航没有宣传的那么长。",
    images: [
      "mackbook.jpg"
    ],
    rating: 4,
    type: 1,
    status: 1,
    reply: "感谢您的反馈，我们会持续优化产品性能。",
    replyTime: 1688385600000
  },
  {
    id: "PJ20230702001",
    createTime: 1688295600000,
    userId: 1003,
    userName: "王五",
    userAvatar: "/images/avatar3.webp",
    productId: "P003",
    productName: "iPad Pro 2022",
    productImage: "/images/ipad.webp",
    content: "物流速度很快，包装完好，使用体验很好。",
    images: [],
    rating: 5,
    type: 1,
    status: 0,
    reply: ""
  },
  {
    id: "PJ20230702002",
    createTime: 1688382000000,
    userId: 1004,
    userName: "赵六",
    userAvatar: "/images/avatar4.webp",
    productId: "P004",
    productName: "AirPods Pro",
    productImage: "/images/airpods.webp",
    content: "降噪效果一般，与宣传不符，有点失望。",
    images: [],
    rating: 2,
    type: 1,
    status: 1,
    reply: "您好，降噪效果可能因使用环境和个人感受有所不同，建议您检查耳机是否佩戴正确。"
  },
  {
    id: "PJ20230703001",
    createTime: 1688468400000,
    userId: 1005,
    userName: "钱七",
    userAvatar: "/images/avatar5.webp",
    productId: null,
    productName: null,
    productImage: null,
    content: "客服态度非常好，解决问题很专业！",
    images: [],
    rating: 5,
    type: 2,
    status: 1,
    reply: "感谢您的认可，我们会继续提供优质服务！"
  }
]);

// 获取所有评价
export const getReviews = () => {
  return [...reviews];
};

// 获取评价详情
export const getReviewById = (id) => {
  return reviews.find(review => review.id === id);
};

// 更新评价状态
export const updateReviewStatus = (id, newStatus) => {
  const review = reviews.find(review => review.id === id);
  if (review) {
    const oldStatus = review.status;
    review.status = newStatus;
    vToast({ 
      message: `评价状态已更新: ${reviewStatusMap[oldStatus]} → ${reviewStatusMap[newStatus]}`,
      position: "top"
    });
    return true;
  }
  return false;
};

// 回复评价
export const replyToReview = (id, replyContent) => {
  const review = reviews.find(review => review.id === id);
  if (review) {
    review.reply = replyContent;
    review.replyTime = new Date().getTime();
    vToast({ message: "回复已提交" });
    return true;
  }
  return false;
};

// 删除评价
export const deleteReview = (id) => {
  const index = reviews.findIndex(review => review.id === id);
  if (index !== -1) {
    reviews.splice(index, 1);
    vToast({ message: "评价已删除" });
    return true;
  }
  return false;
};

// 获取评价统计数据
export const getReviewStats = () => {
  const stats = {
    total: reviews.length,
    published: reviews.filter(r => r.status === 1).length,
    pending: reviews.filter(r => r.status === 0).length,
    hidden: reviews.filter(r => r.status === 2).length,
    averageRating: 0
  };
  
  const publishedReviews = reviews.filter(r => r.status === 1);
  if (publishedReviews.length > 0) {
    const totalRating = publishedReviews.reduce((sum, review) => sum + review.rating, 0);
    stats.averageRating = (totalRating / publishedReviews.length).toFixed(1);
  }
  
  return stats;
};